var PandaView = function() {
	var self = this;
	var lock = false;
	var copy = ["Way out",
				"Loving that grapefruit",
				"Dude, don’t look at me, look at the stars",
				"Turn off the lights!",
				"This is universally amazing",
				"Intergalactastic!",
				"There’s some room up there just past Alpha Aurigae",
				"You’re a star baby",
				"That doodle looks like a noodle",
				"There’s a Bullock up here somewhere",
				"Dude, where’s my spaceship",
				"I like the lights out",
				"You’re weird, i like you",
				"That thing could be anything",
				"I’m like all over the space",
				"I don’t know what I’m doing up here",
				"This air tastes like alien",
				"My teeth are furry",
				"There’s a bug in my eye",
				"Stop clicking me it tickles",
				"Touch me again",
				"You get me high baby",
				"I feel light headed",
				"Gosh it’s romantic tonight",
				"I think my heart skipped a beat",
				"Draw something you spacemonkey",
				"Iam rather dashing in this spacesuit",
				"Stars in your eyes",
				"Pluto is def the coolest",
				"Saturn gets me heated",
				"Don’t mess with my fur",
				"Gosh darn I’m bamboozled",
				"Clear skies with a chance of floating pandas.",
				"How about a 360 pando-rama feature?",
				"Pandonauts are Go.",
				"Anyone seen a shuttle, or some bamboo?",
				"It’s Pandemonium!",
				"I’m just a pandomime dame in a spacesuit, what’s odd about that?",
				"Stop pandarin and start drawing"];


	this.changed = new signals.Signal();


	this.init = function() {
		TweenMax.to($('#panda'), 0, {css:{top: $(window).height()}})
		position()
		$('#panda').click(speak)
	}

	function speak(event) {
		if(!lock){
			lock = true;
			var index = Math.floor(random(0,copy.length-1))
			animateText(copy[index])	
		}
	}

	function position() {
		var px = Math.floor(random(30, $(window).width() - 200))
		var py = Math.floor(random(150, $(window).height() - 140))
		TweenMax.to($('#panda'), random(20,25), {css:{left: px, top: py, shortRotation:random(-40,40)}, onComplete:position, ease: Back.easeOut})
	}

	this.destroy = function () {
		$('#panda').remove();
	}

	function getSize(st) {
	    var holder = document.createElement("canvas")
	    holder.style['position'] = 'absolute';
	    holder.style['left'] = -500+'0px';
	    holder.style['top'] = -500+'0px';
	    
	    holder.height = 25;
	    canvas.appendChild(holder);

	    var c6_context = holder.getContext("2d");
	    c6_context.fillStyle = '#000000';
	    c6_context.font = '11px Roboto';
	    c6_context.textBaseline = 'bottom';
	    var metrics = c6_context.measureText(st);
	    $(holder).remove();
	    return metrics.width + 8;
	}

	function createDialog(st, xpos, ypos) {
	    // console.log("createDialog")

	    var holder = document.createElement("canvas")
	    holder.style['position'] = 'absolute';
	    holder.style['left'] = xpos+'px';
	    holder.style['top'] = ypos+'px';
	    
	    holder.height = 25;
	    

	    var c6_context = holder.getContext("2d");
	    c6_context.fillStyle = '#000000';
	    c6_context.font = '11px Roboto';
	    c6_context.textBaseline = 'bottom';
	    var metrics = c6_context.measureText(st);
	    holder.width = metrics.width + 8
	    c6_context.fillStyle = '#000000';
	    c6_context.fillRect(0,0,metrics.width+8,holder.height-3)
	    c6_context.fillStyle = '#ffffff';
	    c6_context.font = '11px Roboto';
	    c6_context.textBaseline = 'bottom';
	    c6_context.fillText(st, 4, 19);

	    return holder;

	}

	function animateText(st) {
	    var str=trimst(st);
	    var n=str.split(" ");
	    var ix = 57
	    var iy = -20
	   
	    var d = 0;
	    var off =0
	    var mc;
	    var a = []
	    for (var i = 0; i < n.length; i++) {

	        if(ix >=150) {
	            ix = 57
	            iy += 24;
	        }
	        mc = createDialog(n[i],ix,iy)
	        a.push(mc)
	        TweenMax.to(mc, 0, {css:{opacity:0, top:iy+5, shortRotation:random(-10,10)}});
	        TweenMax.to(mc, 0.3, {css:{opacity:1, top:iy, shortRotation:0},delay:d, ease:Quad.easeOut});
	        $('#panda').append(mc)
	        ix += getSize(n[i]) + 1
	        d+=0.1
	        
	    };
	    TweenMax.allTo(a, 0.3, {css:{opacity:0},delay:d+2, ease:Quad.easeOut}, 0.05, removeCopy, [a])
	}

	function removeCopy(a) {

	    for (var i = 0; i < a.length; i++) {
	        $(a[i]).remove();
	    };
	    lock = false
	}

	function trimst(str) {
	    str = str.replace(/^\s+/, '');
	    for (var i = str.length - 1; i >= 0; i--) {
	        if (/\S/.test(str.charAt(i))) {
	            str = str.substring(0, i + 1);
	            break;
	        }
	    }
	    return str;
	}


    return this

}
